package com.teamb6.shop.controller.admin;

import com.teamb6.shop.domain.SearchResponse;
import com.teamb6.shop.domain.User;
import com.teamb6.shop.service.UserService;
import com.teamb6.shop.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/customers")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/search")
    public String userPage(){return "user-list";}

    @RequestMapping("/api/search")
    @ResponseBody
    public SearchResponse searchUserResponse(@RequestParam("pageNum")Integer pageNum,
                                             @RequestParam("pageSize")Integer pageSize, @RequestParam("keyword")String keyword){
        return userService.listUser(pageNum,pageSize,keyword);
    }

    //删除单行
    @DeleteMapping("/delete")
    @ResponseBody
    public Msg deleteUserPage(@RequestParam("id")Integer id){
        userService.deleteUserByPrimaryKey(id);
        return Msg.success("删除用户信息成功");
    }
    //删除选中行，批量删除
    @RequestMapping("/multiDel")
    @ResponseBody
    public Msg deleteUsersPage(@RequestParam("ids")String ids){
        userService.multiDelUsers(ids);
        return Msg.success("删除用户信息成功");
    }

    //得到选中的用户信息
    @GetMapping("/get")
    @ResponseBody
    public User getUserPage(@RequestParam("id")Integer id){
        return userService.findUserByPrimaryKey(id);
    }

    //将编辑后的用户信息保存
    @PostMapping("/edit")
    @ResponseBody
    public Msg editUserPage(User user){
        userService.editUser(user);
        return Msg.success("用户信息已修改");
    }

}
